package com.fox0g.service.impl;



import com.fox0g.mapper.FarmMapper;
//import com.fox0g.mapper.TbShengxiangMapper;
import com.fox0g.mapper.FreshfoodMapper;
import com.fox0g.pojo.Farm;
import com.fox0g.pojo.Freshfood;
import com.fox0g.service.FarmService;
import com.fox0g.service.FreshfoodService;
import com.fox0g.websocket.WebSocketHandler;
import com.fox0g.netty.NettyServerHandler;
import com.fox0g.pojo.PageBean;
//import com.fox0g.service.ShengxiangService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * @program: template-8.1-project
 * @Date: 2023/8/1 14:34
 * @Author: Huang
 * @Description:
 */
@Service
class FreshfoodServiceImp implements FreshfoodService {
    Farm farm = new Farm();
    Freshfood freshfood = new Freshfood();
    ArrayList<String> list = new ArrayList<>();
    //    @Autowired
//    ShengxiangService shengxiangService;
    @Autowired
    FarmService farmService;

    @Autowired
    FarmMapper farmMapper;


    @Autowired
    FreshfoodService freshfoodService;
    @Autowired
    FreshfoodMapper freshfoodMapper;
    @Autowired
    WebSocketHandler webSocketHandler;
    @Autowired
    NettyServerHandler nettyServerHandler;



    @Override
    public void HandleMessage(String message) throws IOException {


        webSocketHandler.send(message);
//        解析温湿度光照强度数据帧
//        if (message.matches("HHT[0-9]+.[0-9]H[0-9]+.[0-9]LS[0-9]+Z")) {
//
//            String arrnews1[] = message.split("T");
//            String arrnew2[] = arrnews1[1].split("H");
//            String arrnew3[] = arrnew2[1].split("LS");
//            String arrnew4[] = arrnew3[1].split("Z");
//            String T = arrnew2[0];  //截取温度
//            String H = arrnew3[0];  //截取湿度
//            String LS = arrnew4[0]; //截取光敏度
////            LocalDateTime now = LocalDateTime.now(); //获取当前时间
//            LocalDateTime now = LocalDateTime.now(); //获取当前时间
//            //向前端发送这些数据帧
//            webSocketHandler.send("t" + arrnew2[0]);
//            webSocketHandler.send("Hd" + arrnew3[0]);
//            webSocketHandler.send("ls" + arrnew4[0]);
//            //将解析后的数据设置给pojo类
//            farm.setTState(T);
//            farm.setHState(H);
//            farm.setLsState(LS);
//            farm.setCreateTime(now);
//            if(Meg != message) {
//                farmMapper.insert(farm);
//                Meg = message;
//            }
//        }


        //解析温度光强阈值数据帧
        if(message.matches("HWt1[0-9]+t2[0-9]+ls[0-9]+Z")){
            String arrnew1[] = message.split("t");
            String arrnew2[] = arrnew1[2].split("ls");
            String arrnew3[] = arrnew2[1].split("Z");
            webSocketHandler.send("T1" + arrnew1[1].substring(1));
            webSocketHandler.send("T2" + arrnew2[0].substring(1));
            webSocketHandler.send("LS1" + arrnew3[0]);

        }

        String cardmatch = "PID[0-9A-Z]+Z";
        if(message.matches(cardmatch)){
            String card = message.substring(0,message.length() - 1);
            Integer integer = freshfoodMapper.selectID();
            Freshfood select = freshfoodMapper.select(integer);
            select.setVid("202122");
            select.setPid(card);
            LocalDateTime now = LocalDateTime.now();
            select.setCreateTime(now);
            freshfoodMapper.insert(select);
        }


        //HHI00.0O00.0LS00.0
        //        解析两个温度和光照强度数据帧
        if (message.matches("HWt1[0-9]+.[0-9]t2[0-9]+.[0-9]ls[0-9]+st[0-9]+Z")) {

            String arrnews1[] = message.split("t1");
            String arrnew2[] = arrnews1[1].split("t2");
            String arrnew3[] = arrnew2[1].split("ls");
            String arrnew4[] = arrnew3[1].split("st");
            String arrnew5[] = arrnew4[1].split("Z");
            String T = arrnew2[0];  //截取温度一
            String H = arrnew3[0];  //截取温度二
            String LS = arrnew4[0]; //截取光敏度
            String Alarm = arrnew5[0]; //警报状态
            System.out.println(T + "  " +H +"  "+ LS + "  "+ " "+ " " + Alarm);
//            LocalDateTime now = LocalDateTime.now(); //获取当前时间
            LocalDateTime now = LocalDateTime.now(); //获取当前时间
            //向前端发送这些数据帧
            webSocketHandler.send("t" + arrnew2[0]);
            webSocketHandler.send("Hd" + arrnew3[0]);
            webSocketHandler.send("ls" + arrnew4[0]);
            //将解析后的数据设置给pojo类
            Integer integer = freshfoodMapper.selectID();
            Freshfood freshfood = freshfoodMapper.select1(integer);
            if (freshfood!= null) {
                freshfood.setPid(freshfood.getPid());
            }
            freshfood.setVid("202122");
            freshfood.setTin(T);  //设置温度一
            freshfood.setTout(H); //设置温度二
            freshfood.setLxin(LS); //设置光照强度
            freshfood.setAlarm("无故障");
            freshfood.setCreateTime(now);

            freshfoodMapper.insert(freshfood);


            }
        }









    @Override
    public PageBean page(Integer page, String cqg, String begin, String end) {
        PageHelper.startPage(page, 5);
        List<Farm> farms = farmMapper.selectList(null);
        Page<Farm> p = (Page<Farm>) farms;
        System.out.println("总记录数" + p.getTotal());
        return new PageBean(p.getTotal(), p.getResult());

    }






}

